package cn.stylefeng.guns.sys.modular.plotInfo.controller;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.communityInfo.model.params.SqInfoParam;
import cn.stylefeng.guns.sys.modular.communityInfo.model.result.SqInfoResult;
import cn.stylefeng.guns.sys.modular.importHelper.*;
import cn.stylefeng.guns.sys.modular.plotInfo.entity.XqInfo;
import cn.stylefeng.guns.sys.modular.plotInfo.mapper.XqInfoMapper;
import cn.stylefeng.guns.sys.modular.plotInfo.model.params.XqInfoParam;
import cn.stylefeng.guns.sys.modular.plotInfo.model.result.XqInfoResult;
import cn.stylefeng.guns.sys.modular.plotInfo.service.XqInfoService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.kernel.model.response.ResponseData;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;


/**
 * 小区表控制器
 *
 * @author WangZhen
 * @Date 2021-01-21 09:42:34
 */
@Controller
@RequestMapping("/xqInfo")
public class XqInfoController extends BaseController {

    private String PREFIX = "/modular/plotInfo/xqInfo";

    @Autowired
    private XqInfoService xqInfoService;

    @Autowired
    private XqInfoMapper xqInfoMapper;

    /**
     * 跳转到主页面
     *
     * @author WangZhen
     * @Date 2021-01-21
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/xqInfo.html";
    }

    /**
     * 新增页面
     *
     * @author WangZhen
     * @Date 2021-01-21
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/xqInfo_add.html";
    }

    /**
     * 编辑页面
     *
     * @author WangZhen
     * @Date 2021-01-21
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/xqInfo_edit.html";
    }

    /**
     * 新增接口
     *
     * @author WangZhen
     * @Date 2021-01-21
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(XqInfoParam xqInfoParam) {
        this.xqInfoService.add(xqInfoParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author WangZhen
     * @Date 2021-01-21
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(XqInfoParam xqInfoParam) {
        this.xqInfoService.updatePlotInfo(xqInfoParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author WangZhen
     * @Date 2021-01-21
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(XqInfoParam xqInfoParam) {
        this.xqInfoService.delete(xqInfoParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author WangZhen
     * @Date 2021-01-21
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(XqInfoParam xqInfoParam) {
        XqInfoResult detail = this.xqInfoService.findListBySpec(xqInfoParam).get(0);
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author WangZhen
     * @Date 2021-01-21
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(XqInfoParam xqInfoParam) {
        return this.xqInfoService.findPageBySpec(xqInfoParam);
    }

    /**
     * 查询列表
     *
     * @author author
     * @Date 2021-01-06
     */
    @ResponseBody
    @RequestMapping("/listAll")
    public List<XqInfoResult> listAll(XqInfoParam xqInfoParam) {
        return this.xqInfoService.findListBySpec(xqInfoParam);
    }

    /**
     * 通过user查询小区列表
     *
     * @author author
     * @Date 2021-01-06
     */
    @ResponseBody
    @RequestMapping("/listXqByUser")
    public List<XqInfoResult> listXqByUser() {
        return this.xqInfoMapper.customListByUser(LoginContextHolder.getContext().getUserId());
    }

    /**
     * 删除接口
     */
    @ResponseBody
    @RequestMapping("/allDelete")
    public Integer allDelete(@RequestParam Integer[] ids) {
        Integer returnNumber = 0;
        for (int i = 0; i < ids.length; i++) {
            this.xqInfoService.selectDeleteInfo(ids[i]);
            returnNumber++;
        }
        return returnNumber;
    }

    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;

    /**
     * 导入数据
     *
     * @param file
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/importData_new")
    public ResponseData importData_new(MultipartFile file, String relSqId) throws IOException {
        String userName = LoginContextHolder.getContext().getUser().getName();
        String createTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        ExcelConfig excelConfig = new ExcelConfig(file, 0, 1, -1, 0, 6, "project_wxjj.wxjj_xq_info");
        excelConfig.setBatchSize(100);
        IBaseExcel ii = new BaseExcelHandle() {

            @Override
            protected int checkRow(Object[] row, DataStore data, IContext context) {
                // TODO Auto-generated method stub
//				System.out.println("外部打印：" + Arrays.toString(row));
                Object[] temp = data.getData();
                temp[0] = null;
                temp[1] = "0";
                temp[2] = row[0];
                temp[3] = row[1];
                temp[4] = relSqId;
                if (row[2].equals("是")) {
                    temp[5] = "1";
                } else {
                    temp[5] = "0";
                }
                temp[6] = row[3];
                temp[7] = row[4];
                temp[8] = row[5];
                temp[9] = row[6];
                temp[10] = userName;
                temp[11] = createTime;
                data.setData(temp);
                return BaseExcelHandle.GO_CONTINUE;
            }

            @Override
            protected Object beforeHandle() {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            protected Object afterHandle() {
                // TODO Auto-generated method stub
                return null;
            }

        };
        ii.setSqlSessionTemplate(sqlSessionTemplate);
        ii.run(excelConfig);
        return ResponseData.success(400, "导入成功", null);

    }

}


